<div ng-controller="Customer_MyAccountListC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">户头管理</h1>
    </div>
    <div class="wrapper-md" style="text-align: right;">
        <div class="panel panel-default">
            <div class="panel-heading">
                <button class="btn btn-sm btn-default" ng-click="SetNowEdit(null,true)" data-toggle="modal" data-target="#dlg">
                    <i class="fa fa-plus text"></i>
                    <span class="text">添加</span>
                </button>
                <button type="button" class="btn btn-default" ng-click="goback()">返回</button>
            </div>

            <div class="panel panel-default text-left" style="margin-bottom: 10px" ng-repeat="item in data.Accounts">
                <a ng-click="SetNowEdit(item)" data-toggle="modal" data-target="#dlg">
                    <div class="panel-heading">
                        <h3 class="panel-title">{{item.Name}}</h3>
                    </div>
                    <div class="panel-body ">
                        <div class="row">
                            <div class="col-xs-12"><strong>银行：</strong>{{item.BankName}}</div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12"><strong>账号：</strong>{{item.BankNumber}}</div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12"><strong>备注：</strong>{{item.Remark}}</div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12"><strong>账户余额：</strong>{{item.Amount | number:2}}</div>
                        </div>
                    </div>
                </a>
                <div class="panel-footer">
                    <div class="row text-right">
                        <button ng-disabled="item.Amount<1 " style="margin: 5px 10px" class="btn btn-sm btn-default" ng-click="refund(item)">
                            <span class="text">申请退款</span>
                        </button>
                        <button style="margin: 5px 10px" class="btn btn-sm btn-default" ng-click="addpay(item)">
                            <i class="fa fa-plus text"></i>
                            <span class="text">报款</span>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="dlg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button ng-click="abolish()" type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">账户信息</h4>
                </div>
                <form name="form" class="form-validation">
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="recipient-name" class="control-label">名称(全称):</label>
                            <input ng-disabled="NowData.Name == '现金户头'" required type="text" class="form-control" id="recipient-name" ng-model="NowData.Name">
                        </div>
                        <div class="form-group">
                            <label class="control-label">银行</label>
                            <input required type="text" class="form-control" ng-model="NowData.BankName">

                        </div>
                        <div class="form-group">
                            <label class="control-label">帐号(只能是数字)</label>
                            <input required type="text" class="form-control" ng-model="NowData.BankNumber" ng-pattern="/^[0-9]*$/">
                        </div>
                        <div class="form-group">
                            <label class="control-label">备注(非必填)</label>
                            <input type="text" class="form-control" ng-model="NowData.Remark">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" ng-disabled="form.$invalid" data-dismiss="modal" ng-hide="NowData.isNew" ng-click="edit()">修改</button>
                        <button type="button" class="btn btn-default" ng-disabled="form.$invalid" data-dismiss="modal" ng-show="NowData.isNew" ng-click="add()">新建</button>
                        <button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    angular.module('app')
            .controller("Customer_MyAccountListC", function ($scope, $state, $http, $stateParams) {
                $scope.backUrlFrom = $stateParams.from;
                $scope.id = $stateParams.id;
                $http.get("/api/Customer/GetAccountsList", { params: { "id": $stateParams.id } })
                        .then(function (res) {
                            $scope.data = res.data;
                            console.log($scope.data);
                        })
                        .then(function (error) {
                            if (error) {
                                alert("数据获取失败");
                            }
                        });
                $scope.SetNowEdit = function (data, isNew) {
                    if (isNew) {
                        $scope.NowData = {};
                        $scope.NowData.isNew = true;
                        $scope.NowData.CustomerId = $stateParams.id;
                    } else {
                        $scope.NowData = data;
                    }

                };
                $scope.edit = function () {
                    console.log($scope.NowData);
                    $http.put("../api/Customer/EditAccount", $scope.NowData)
                            .success(function (res) {
                                if (res.State <= 0) {
                                    alert(res.Msg);
                                } else {
                                    alert("修改成功");
                                    $state.go('app.Customer_MyAccountList', { 'id': $stateParams.id });
                                }
                            });
                };
                $scope.add = function () {
                    console.log($scope.NowData);
                    $http.post("../api/Customer/AddAccount?customerId=" + $scope.NowData.CustomerId, $scope.NowData)
                            .success(function (res) {
                                if (res.State <= 0) {
                                    alert(res.Msg);
                                } else {
                                    alert("保存成功");
                                    $state.go('app.Customer_MyAccountList', { 'id': $stateParams.id });
                                }
                            });
                };
                $scope.refund = function (item) {
                    debugger;
                    item.Amount = item.Amount.toFixed(2);
                    if (confirm("退款金额：" + item.Amount + ",确认提交退款申请?")) {
                        $http({
                            url: "../api/ClientManager/ApplyRefund?AccountId=" + item.Id,
                            method: "POST"
                        })
                                .success(function (res) {
                                    if (res.State <= 0) {
                                        alert(res.Msg);
                                    } else {
                                        alert("已提交退款申请");
                                        $state.go('app.Customer_MyAccountList', { 'id': $stateParams.id });
                                    }
                                })
                    }
                };
                $scope.goback = function () {
                  
                    if ($scope.backUrlFrom == "0") {
                        $state.go('app.Customer_SelfList');
                    } else if ($stateParams.from == "1") {
                        $state.go('app.Customer_Detail', { 'id': $stateParams.id });
                    }
                };
                $scope.addpay = function (item) {
                    $state.go('app.ClientManager_PaymentInfo', {
                        id: JSON.stringify(item),
                        from: $scope.backUrlFrom
                    });

                };
            })
</script>
